﻿Imports System.Data

Partial Class AdministrarTurnos
    Inherits System.Web.UI.Page


    Protected Sub btnAgregar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        Try
            TurnosDB.AsignarTurno(ddlEmpleados.SelectedValue, ddlTurnos.SelectedValue, calFecha.SelectedDate)
        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "AdministrarTurnos.aspx"
            Server.Transfer("Errores.aspx")
        End Try
        cargarGrillaTurnosEmpleados()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            cargarDataListTurnos()
            CargarDataListEmpleados()
            calFecha.SelectedDate = Now
            cargarGrillaTurnosEmpleados()
        End If
        
    End Sub
    Sub cargarGrillaTurnosEmpleados()
        gvTurnosEmpleados.DataSource = TurnosDB.GetTurnosEmpleado(ddlEmpleados.SelectedValue)
        gvTurnosEmpleados.DataBind()
    End Sub

    Sub cargarGrillaTurnosEmpleadosPorMes()
        gvTurnosEmpleados.DataSource = TurnosDB.GetTurnosByMes(calFecha.SelectedDate.Month)
        gvTurnosEmpleados.DataBind()
    End Sub
    Sub cargarDataListTurnos()
        Dim ds As New DataSet
        ds = TurnosDB.GetTurnos
        With ddlTurnos
            .DataSource = ds.Tables("Turnos")
            .DataValueField = "id_turno"
            .DataTextField = "Turno"
            .DataBind()
        End With
    End Sub

    Sub CargarDataListEmpleados()
        Dim ds As New DataSet
        ds = EmpleadosDB.GetEmpleados
        With ddlEmpleados
            .DataSource = ds.Tables("Empleados")
            .DataValueField = "id_empleado"
            .DataTextField = "apellido_nombre"
            .DataBind()
            .Items.Insert(0, New ListItem("Todos", "0"))
        End With

    End Sub

    Protected Sub gvTurnosEmpleados_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvTurnosEmpleados.PageIndexChanging
        gvTurnosEmpleados.PageIndex = e.NewPageIndex
        cargarGrillaTurnosEmpleados()
    End Sub

    Protected Sub gvTurnosEmpleados_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvTurnosEmpleados.RowDeleting
        Try
            TurnosDB.EliminarTurnoEmpleado(Convert.ToInt32(gvTurnosEmpleados.DataKeys(e.RowIndex).Values(0).ToString()), Convert.ToInt32(gvTurnosEmpleados.DataKeys(e.RowIndex).Values(1).ToString()), Format(CDate(gvTurnosEmpleados.DataKeys(e.RowIndex).Values(2).ToString()), "dd/MM/yyyy"))
        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "AdministrarTurnos.aspx"
            Server.Transfer("Errores.aspx")
        End Try

        cargarGrillaTurnosEmpleados()
    End Sub

    Protected Sub gvTurnosEmpleados_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvTurnosEmpleados.SelectedIndexChanged

    End Sub

    Protected Sub calFecha_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles calFecha.SelectionChanged
        cargarGrillaTurnosEmpleadosPorMes()
    End Sub

    Protected Sub ddlEmpleados_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlEmpleados.SelectedIndexChanged
        cargarGrillaTurnosEmpleados()
    End Sub
End Class
